import Vue from 'vue'
import Vuex from 'vuex'
import { getMenuList } from "@/api"
import allRoutes from "../router/allRoutes"
import recursionRoutes from "../utils/recursionRoutes"
import dynamicRoutes from "../router/dynamicRoutes"
import router from "../router"
Vue.use(Vuex)
let userInfo = localStorage.getItem("wf-userInfo") || '{}';
userInfo = JSON.parse(userInfo)
export default new Vuex.Store({
  state: {
    userInfo,
    menuList: [], 
  },
  mutations: {
    SET_USERINFO(state, payload) {
      state.userInfo = payload;
    },
    SET_MENULIST(state, payload) {
      state.menuList = payload;
      let target = dynamicRoutes.find(item => item.path === "/")
      target.children = [...state.menuList]
      router.addRoutes(dynamicRoutes)
    },
    SET_CRUMBS(state, payload) {
      state.crumbs = payload
    }
  },
  actions: {
    async FETCH_MENULIST({ commit }) {
      let userMenu = await getMenuList()
      let sideMenu = recursionRoutes(allRoutes, userMenu.data.menuList)
      commit("SET_MENULIST", sideMenu)
    }
  },
  modules: {
  }
})
